package services;

import dao.DeliveryPlan_Repository;
import models.*;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;

public class DeliveryPlan_Repository_JPA implements DeliveryPlan_Repository {

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public DeliveryPlan_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    public CompletionStage<List<DeliveryPlan_F_Entity>> getDeliPlanList (String BillNo)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql = "select * from f_TabDeliveryPlan ('"+BillNo+"')";
                return entityManager.createNativeQuery(strSql, DeliveryPlan_F_Entity.class).getResultList();
            });
        }, executionContext);
    }

    public CompletionStage<List<Object>> createDeliPlan(String PhoneNumber,String LoadingSubNum, String DeliveryPlace
            , String WeigherCode, String Amount, String StartDate, String EndDate , String TransType , String ShipName
            , String UnloadPlace, String CustomerDept, String ConUser, String BatchCode, String DeliveryNo)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "exec p_UpdateDeliveryPlan @PhoneNumber = '"+PhoneNumber+"',@LoadingSubNum = '"
                        + LoadingSubNum + "', @DeliveryPlace = '"
                        + DeliveryPlace + "', @WeigherCode = '"
                        + WeigherCode +  "', @Amount = "
                        + Amount +",@StartDate ='"
                        + StartDate +  "', @EndDate ='"
                        + EndDate + "', @TransType ='"
                        + TransType + "', @ShipName ='"
                        + ShipName + "' ,@UnloadPlace ='"
                        + UnloadPlace +  "', @CustomerDept ='"
                        + CustomerDept + "', @ConUser ='"
                        + ConUser + "', @BatchCode ='"
                        + BatchCode + "' , @DeliveryNo ='"
                        + DeliveryNo + "' ";
//            	return entityManager.createNativeQuery(strSql, DeliveryPlan_T_Entity.class).getResultList();

                List<DeliveryPlan_T_Entity> list = entityManager.createNativeQuery(strSql, DeliveryPlan_T_Entity.class).getResultList();

                if(list.size() != 0){
                    String  a="添加成功";
                    List<Object> res = new ArrayList<Object>();
                    res.add(a);
                    return res;
                }else{
                    String strSql2 = "select * from f_GetLastErr('" + PhoneNumber + "') ";
                    return entityManager.createNativeQuery(strSql2, ErrorLog_T_Entity.class).getResultList();
                }
            });
        }, executionContext);
    }

    public CompletionStage<List<DeliveryPlan_T_Entity>> cancleDeliPlan(String PhoneNumber, String StartDate, String EndDate , String DeliveryNo, String LoadingSubNum)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "exec p_UpdateDeliveryPlan @PhoneNumber = '"+PhoneNumber+"',@LoadingSubNum = '"+LoadingSubNum+"', @DeliveryPlace = '', @WeigherCode = '', @Amount = 0,@StartDate ='"
                        + StartDate +  "', @EndDate ='"
                        + EndDate + "', @TransType ='', @ShipName ='' ,@UnloadPlace ='', @CustomerDept ='', @ConUser ='', @BatchCode ='' , @DeliveryNo ='"
                        + DeliveryNo + "' ";
                return entityManager.createNativeQuery(strSql, DeliveryPlan_T_Entity.class).getResultList();
            });
        }, executionContext);
    }

    @Override
    public CompletionStage<List<DeliveryPlan_T_Entity>> editDeliPlan(String PhoneNumber, String Amount, String StartDate, String EndDate, String DeliveryNo, String LoadingSubNum) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "exec p_UpdateDeliveryPlan @PhoneNumber = '"+PhoneNumber+"',@LoadingSubNum = '"+LoadingSubNum+"', @DeliveryPlace = '', @WeigherCode = '', @Amount = '"
                        + Amount +"',@StartDate ='"
                        + StartDate +  "', @EndDate ='"
                        + EndDate + "', @TransType ='', @ShipName ='' ,@UnloadPlace ='', @CustomerDept ='', @ConUser ='', @BatchCode ='' , @DeliveryNo ='"
                        + DeliveryNo + "' ";
                return entityManager.createNativeQuery(strSql, DeliveryPlan_T_Entity.class).getResultList();
            });
        }, executionContext);
    }

    @Override
    public CompletionStage<XMLString_Entity> getDeliveryPlan() {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql = "exec p_GetDeliveryPlan ";
                return (XMLString_Entity)(entityManager.createNativeQuery(strSql, XMLString_Entity.class).getSingleResult());
            });
        }, executionContext);
    }

    @Override
    public CompletionStage<List<Object>> createDeliveryPlan(String PhoneNumber, String LoadingSubNum, String DeliveryPlace, String WeigherCode, String Amount, String StartDate, String EndDate, String TransType, String ShipName, String UnloadPlace, String CustomerDept, String ConUser, String BatchCode, String DeliveryNo, String DeliveryOrderNo) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "exec p_UpdateDeliveryPlan @PhoneNumber = '"+PhoneNumber+"',@LoadingSubNum = '"
                        + LoadingSubNum + "', @DeliveryPlace = '"
                        + DeliveryPlace + "', @WeigherCode = '"
                        + WeigherCode +  "', @Amount = "
                        + Amount +",@StartDate ='"
                        + StartDate +  "', @EndDate ='"
                        + EndDate + "', @TransType ='"
                        + TransType + "', @ShipName ='"
                        + ShipName + "' ,@UnloadPlace ='"
                        + UnloadPlace +  "', @CustomerDept ='"
                        + CustomerDept + "', @ConUser ='"
                        + ConUser + "', @BatchCode ='"
                        + BatchCode + "' , @DeliveryNo ='"
                        + DeliveryNo + "', @DeliveryOrderNo = '"
                        +DeliveryOrderNo+"' ";
//            	return entityManager.createNativeQuery(strSql, DeliveryPlan_T_Entity.class).getResultList();

                List<DeliveryPlan_T_Entity> list = entityManager.createNativeQuery(strSql, DeliveryPlan_T_Entity.class).getResultList();

                if(list.size() != 0){
                    String  a="添加成功";
                    List<Object> res = new ArrayList<Object>();
                    res.add(a);
                    return res;
                }else{
                    String strSql2 = "select * from f_GetLastErr('" + PhoneNumber + "') ";
                    return entityManager.createNativeQuery(strSql2, ErrorLog_T_Entity.class).getResultList();
                }
            });
        }, executionContext);
    }

    @Override
    public CompletionStage<XMLString_Entity> getDeliveryPlanPage(Integer RowId, Integer Length) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql = "exec p_GetDeliveryPlan_Page "+RowId+","+Length+" ";
                return (XMLString_Entity)(entityManager.createNativeQuery(strSql, XMLString_Entity.class).getSingleResult());
            });
        }, executionContext);
    }

    @Override
    public CompletionStage<List<Object>> createDeliveryOrder(String PhoneNumber, Integer DeliveryPlanNo, String DeliveryOrderNo, String DeliveryPlace, String WeigherCode, String Amount, String StartDate, String EndDate, String TransType, String ShipName, String UnloadPlace, String CustomerDept, String ConUser, String BatchCode) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "exec p_CreateDeliveryOrder @PhoneNumber = '"
                        + PhoneNumber +"',@DeliveryPlanNo = '"
                        + DeliveryPlanNo + "',@DeliveryOrderNo = '"
                        + DeliveryOrderNo +"', @DeliveryPlace = '"
                        + DeliveryPlace + "', @WeigherCode = '"
                        + WeigherCode +  "', @Amount = "
                        + Amount +",@StartDate ='"
                        + StartDate +  "', @EndDate ='"
                        + EndDate + "', @TransType ='"
                        + TransType + "', @ShipName ='"
                        + ShipName + "' ,@UnloadPlace ='"
                        + UnloadPlace +  "', @CustomerDept ='"
                        + CustomerDept + "', @ConUser ='"
                        + ConUser + "', @BatchCode ='"
                        + BatchCode + "'";
//            	return entityManager.createNativeQuery(strSql, ApplyInfo_T_Entity.class).getResultList();

                List<DeliveryOrder_T_Entity> list =
                        entityManager.createNativeQuery(strSql, DeliveryOrder_T_Entity.class).getResultList();

                if(list.size() != 0) {
//                    String  a="添加成功";
                    List<Object> res = new ArrayList<Object>();
                    res.add(list);
                    return res;

                }else {
                    String strSql2 = "select * from f_GetLastErr('" + PhoneNumber + "') ";
                    return entityManager.createNativeQuery(strSql2, ErrorLog_T_Entity.class).getResultList();
                }
            });
        }, executionContext);
    }


}
